//
//  NetWorkingTool.m
//  网络基础08
//
//  Created by 花花蔡 on 2016/12/6.
//  Copyright © 2016年 张晓林. All rights reserved.
//

#import "NetWorkingTool.h"

@implementation NetWorkingTool

+(instancetype)sharedTools{
    
    static NetWorkingTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //初始化单例类
        instance = [[NetWorkingTool alloc]initWithBaseURL:nil];
        
        //增加解析数据格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
    });
    return instance;
}


//封装自己的网络请求方法
- (void)requestMethod:(ReqestM)method WithURL:(NSString *)url  parameters:(NSDictionary *)parameters callBack:(networkBlock)finished {
    
    if ( method == GET ) {
        //调用afn方法
        [self GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //成功
            //回调参数
            finished(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            //失败
            finished(nil,error);
        }];

    }else {
    
        [self POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            finished(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finished(nil,error);
        }];
        
    }
    
}

@end
